package com.org.csu.service.Impl;

import com.org.csu.dao.HotwordRepository;
import com.org.csu.dto.HotwordDTO;
import com.org.csu.entity.HotWord;
import com.org.csu.entity.Word;
import com.org.csu.service.HotwordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.org.csu.converter.HotwordConverter.convertListHotword;

/**
 * @author YigongLily
 * @version V1.0
 * Copyright (c) 2024, t8669@qq.com All Rights Reserved.
 * @ProjectName:e-commerce-back
 * @Title: HotwordServiceImpl
 * @Package com.org.csu.service.Impl
 * @Description: hotwSimpl
 * @date 2024/12/15 17:47
 */
@Service
public class HotwordServiceImpl implements HotwordService {
    @Autowired
    private HotwordRepository hotwordRepository;


    @Override
    public List<List<HotwordDTO>> getHotword() {
        List<HotWord> hotWords = hotwordRepository.findAll();
        // 根据type属性分类
        List<HotWord> type0Words = hotWords.stream()
                .filter(hotWord -> hotWord.getType() == 0)
                .collect(Collectors.toList());

        List<HotWord> type1Words = hotWords.stream()
                .filter(hotWord -> hotWord.getType() == 1)
                .collect(Collectors.toList());

        List<HotWord> type2Words = hotWords.stream()
                .filter(hotWord -> hotWord.getType() == 2)
                .collect(Collectors.toList());

        // 将HotWord列表转换为HotWordDTO列表
        List<HotwordDTO> type0DTOs = convertListHotword(type0Words);
        List<HotwordDTO> type1DTOs = convertListHotword(type1Words);
        List<HotwordDTO> type2DTOs = convertListHotword(type2Words);

        // 按照012的顺序将三个List<HotWordDTO>放入返回的Object中
        List<List<HotwordDTO>> result = List.of(type0DTOs, type1DTOs, type2DTOs);

        return result;
    }
}